import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from threading import Thread, Event
import util  # 导入工具模块
import logging

logging.basicConfig(level=logging.INFO)

class ImageProcessingApp:
    def __init__(self, root):
        self.root = root
        self.root.title("图像批量处理工具")
        
        # 配置参数
        self.config = util.load_config()
        self.scale_min = self.config.get("scale_min", 0.5)
        self.scale_max = self.config.get("scale_max", 1.5)
        self.num_images = self.config.get("num_images", 10)
        self.output_type = self.config.get("output_type", "RGB")
        
        # 线程控制
        self.thread = None
        self.stop_event = Event()
        
        # 创建界面组件
        self.create_widgets()
    
    def create_widgets(self):
        # ZIP文件选择
        ttk.Label(self.root, text="选择ZIP文件:").grid(row=0, column=0, padx=5, pady=5)
        self.zip_path_var = tk.StringVar()
        ttk.Entry(self.root, textvariable=self.zip_path_var, width=40).grid(row=0, column=1, padx=5, pady=5)
        ttk.Button(self.root, text="浏览", command=self.select_zip_file).grid(row=0, column=2, padx=5, pady=5)
        
        # 缩放范围
        ttk.Label(self.root, text="缩放范围:").grid(row=1, column=0, padx=5, pady=5)
        self.scale_min_entry = ttk.Entry(self.root, width=5)
        self.scale_min_entry.grid(row=1, column=1, padx=5, pady=5)
        self.scale_min_entry.insert(0, self.scale_min)
        ttk.Label(self.root, text="到").grid(row=1, column=2, padx=5, pady=5)
        self.scale_max_entry = ttk.Entry(self.root, width=5)
        self.scale_max_entry.grid(row=1, column=3, padx=5, pady=5)
        self.scale_max_entry.insert(0, self.scale_max)
        
        # 生成数量
        ttk.Label(self.root, text="生成图像数量:").grid(row=2, column=0, padx=5, pady=5)
        self.num_images_entry = ttk.Entry(self.root, width=5)
        self.num_images_entry.grid(row=2, column=1, padx=5, pady=5)
        self.num_images_entry.insert(0, self.num_images)
        
        # 输出类型
        ttk.Label(self.root, text="输出类型:").grid(row=3, column=0, padx=5, pady=5)
        self.output_type_var = tk.StringVar(value=self.output_type)
        ttk.Combobox(
            self.root,
            textvariable=self.output_type_var,
            values=["RGB", "Grayscale", "Binary"]
        ).grid(row=3, column=1, padx=5, pady=5)
        
        # 进度条
        ttk.Label(self.root, text="处理进度:").grid(row=4, column=0, padx=5, pady=5)
        self.progress_bar = ttk.Progressbar(self.root, length=300, mode='determinate')
        self.progress_bar.grid(row=4, column=1, padx=5, pady=5, columnspan=2)
        
        # 按钮
        ttk.Button(self.root, text="开始处理", command=self.start_processing).grid(row=5, column=1, padx=5, pady=10)
        ttk.Button(self.root, text="取消", command=self.cancel_processing).grid(row=5, column=2, padx=5, pady=10)
    
    def select_zip_file(self):
        path = filedialog.askopenfilename(filetypes=[("ZIP文件", "*.zip")])
        if path:
            self.zip_path_var.set(path)
    
    def update_progress(self, value):
        self.progress_bar["value"] = value
        self.root.update_idletasks()
    
    def start_processing(self):
        if not self.zip_path_var.get():
            messagebox.showwarning("警告", "请选择ZIP文件")
            return
        
        try:
            self.scale_min = float(self.scale_min_entry.get())
            self.scale_max = float(self.scale_max_entry.get())
            self.num_images = int(self.num_images_entry.get())
            self.output_type = self.output_type_var.get()
        except ValueError:
            messagebox.showerror("错误", "参数格式错误")
            return
        
        self.stop_event.clear()
        self.thread = Thread(
            target=util.process_images_from_zip,
            args=(
                self.zip_path_var.get(),
                self.scale_min,
                self.scale_max,
                self.num_images,
                self.output_type
            ),
            kwargs={
                "progress_callback": self.update_progress,
                "thread_event": self.stop_event
            }
        )
        self.thread.start()
    
    def cancel_processing(self):
        if self.thread and self.thread.is_alive():
            self.stop_event.set()
            messagebox.showinfo("提示", "处理已取消")

if __name__ == "__main__":
    root = tk.Tk()
    app = ImageProcessingApp(root)
    root.mainloop()